package ImplementChat;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/*客户端：
1. 连接服务器 Socket
2. 发送消息*/
public class TcpClient01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //知道服务器的地址和端口号
            InetAddress serverIP = InetAddress.getByName("localhost");
            int port = 9999;
            //创建一个Socket连接
            socket = new Socket(serverIP,port);
            //发送消息 IO流
            outputStream = socket.getOutputStream();
            outputStream.write("你好，装逼我让你飞起来".getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }



    }
}
